/**
 * 
 */
package org.statewalker.parser.turtle.tokenizer;

import org.statewalker.parser.turtle.TurtleDict1;
import org.statewalker.tokenizer.CharStream;
import org.statewalker.tokenizer.ITokenizer;
import org.statewalker.tokenizer.StreamToken;
import org.statewalker.tokenizer.CharStream.Pointer;
import org.statewalker.tokenizer.base.NewLineTokenizer;

/**
 * @author kotelnikov
 */
public class CommentTokenizer implements ITokenizer {

    public final static CommentTokenizer INSTANCE = new CommentTokenizer();

    private String fKey;

    public CommentTokenizer() {
        this(TurtleDict1.COMMENT);
    }

    /**
     * 
     */
    public CommentTokenizer(String key) {
        fKey = key;
    }

    public StreamToken read(CharStream stream) {
        if (stream.getChar() != '#')
            return null;
        Pointer begin = stream.getPointer();
        StringBuilder buf = new StringBuilder();
        while (!stream.isTerminated()) {
            String eol = NewLineTokenizer.readNewLine(stream);
            if (eol != null) {
                buf.append(eol);
                break;
            } else {
                char ch = stream.getChar();
                buf.append(ch);
            }
            stream.incPos();
        }
        Pointer end = stream.getPointer();
        return new StreamToken(fKey, begin, end, buf.toString());
    }

}
